package edu.princeton.cogsci.wn;

/**
 *  This class models a lexicalized concept, as represented by a set of
 *  synonyms.
 */

public class Synset
{
  /* Instance variables */
 

  /** The part of speech. @see Constants */
  private int pos;

  /** The definition. */
  private String gloss;
  private MarkupText markup;
  
  /** The semantic category. @see Constants */
  private int category;
	
  /** The set of synonyms. */
  private WordSense[] synonyms;

  /** The set of semantic relations. */
  private SemanticRelation[] relations;

  /** The key that uniquely identifies this synset. */
  private SynsetKey key;

  /** The set of verb frames that apply to every synonym. @see Constants */
  private int[] frames;


  /* Accessor methods. */


  /** Returns the part of speech. @see Constants */
  public int getPos( ) { return pos; }
  
//  public char getPosChar( ) { return Constants.posChars[ pos ]; }
//  public String getPosString( ) { return Constants.posStrings[ pos ]; }
  
  /** Returns the definition. */
  public String getGloss( ) { return markup.toString( ); }
  public MarkupText getMarkup( ) { return markup; }
  
  /** Returns the semantic category. @see Constants */
  public int getCategory( ) { return category; }
  
  /** Returns the set of synonyms. */
  public WordSense[] getSynonyms( ) { return synonyms; }
  
  /** Returns the set of semantic relations. */
  public SemanticRelation[] getRelations( ) { return relations; }
  
  /** Returns the key that uniquely identifies this synset. */
  public SynsetKey getKey( ) { return key; }
  
  /** Returns the set of verb frames that apply to every synonym.
   *  @see Constants
   */
  public int[] getFrames( ) { return frames; }


  /* Mutator methods */


  /** Sets the part of speech. @see Constants */
  public void setPos( int pos ) { this.pos = pos; }
  
  /** Sets the definition. */
  public void setGloss( String gloss )
  {
    markup = new MarkupText( gloss );
    gloss = markup.toString( );
  }
  
  /** Sets the semantic category. @see Constants */
  public void setCategory( int category ) { this.category = category; }
  
  /** Sets the set of synonyms. */
  public void setSynonyms( WordSense[] s ) { this.synonyms = s; }
  
  /** Sets the set of semantic relations. */
  public void setRelations( SemanticRelation[] r ) { relations = r; }
  
  /** Sets the key that uniquely identifies this synset. */
  public void setKey( SynsetKey key ) { this.key = key; }
  
  /** Sets the set of verb frames that apply to every synonym.
   *  @see Constants
   */
  public void setFrames( int[] frames ) { this.frames = frames; }


  /* Utility methods */


  /** Returns the word-sense object that equals the given string
   *  representation, or null if none does.
   */
  public WordSense getWordSense( String word )
  {
    word = WordNetImpl.toCanonicalForm( word );
    for ( int i = 0; i < synonyms.length; i++ )
    {
      String cWord = WordNetImpl.toCanonicalForm( synonyms[i].toString( ) );
      if ( cWord.equals( word ) )
        return synonyms[i];
    }
    return null;
  }

  /** Returns the word-sense object at the head of the synonym list, or null
   *  if the list is empty.
   */
  public WordSense getHead( )
  {
    if ( synonyms.length > 0 )
      return synonyms[0];
    else
      return null;
  }
  
  /** Returns an HTML representation of this synset, showing the synonyms
   *  and the gloss.
   */
  public String toHTML( )
  {
    return "<html><font size=2><b>" + toString( ) + "</b> <font color=gray>" +
           getGloss( ) + "</font></font></html>";
  }

  /** Returns a string representation of this synset, showing all the
   *  synonyms. */
  public String toString( )
  {
    return toString( 0 );
  }

  /** Returns a string representation of this synset, showing <CODE>n</CODE>
   *  synonyms.
   */
  public String toString( int n )
  {
    int i = 0;
    String ret = "";
    if ( n == 0 || n > synonyms.length )
      n = synonyms.length;
    ret = "{";
    for ( i = 0; i < n; i++ )
    {
      ret += synonyms[i];
      if ( i < ( synonyms.length - 1 ) )
        ret += ", ";
    }
    if ( n < synonyms.length )
      ret += "...";
    ret += "}";
    return ret;
  }
}
